-- FUNCTION: public.widget_BarChart_Pharmacy_Sale_Revenue(date, text, integer,integer,integer)

-- DROP FUNCTION IF EXISTS public."widget_BarChart_Pharmacy_Sale_Revenue"(date, text, integer,integer,integer);

CREATE OR REPLACE FUNCTION public."widget_BarChart_Pharmacy_Sale_Revenue"(
	"fromDate" date,
	"filterType" text,
	"displayCount" integer,
	"referenceId" integer DEFAULT NULL::integer,
	"locationId" integer DEFAULT NULL::integer)
    RETURNS TABLE("Date" date, "Count" numeric) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
declare
f record;
filtervalue interval;
intervaldata interval;
fromdate date;
begin
create  Temp TABLE temp_table 
( saledate date,
  pharmacyamount numeric
 ) ON COMMIT DELETE ROWS;
 filtervalue=  "displayCount"||' '|| "filterType";
 intervaldata= '1 '|| "filterType";

raise notice 'filtervalue %', filtervalue;

--fromdate:=case when "filterType"='day' then "fromDate" 
 --else date_trunc('month', "fromDate"::date) end;
 
-- raise notice 'currentdate %', "fromdate"+filtervalue;
--  raise notice 'startdate %', fromdate;

for f in SELECT generate_series("fromDate"-filtervalue, "fromDate"+filtervalue,  intervaldata) weeks
    loop 
-- 	raise notice 'loop interval %', intervaldata;
-- 	raise notice 'loop fromdate %', f.weeks;
	insert into temp_table (saledate,pharmacyamount)																  

with PharmaSaleAmount as (
	
select a."AccountId", 
	sum(a."Cash") "PharmaSaleCash",
	sum(a."Card") "PharmaSaleCard",
	sum(a."Upi") "PharmaSaleUpi",
	sum(a."Online") "PharmaSaleOnline",
	sum(a."Cheque") "PharmaSaleCheque",
	sum(a."Paytm") "PharmaSalePaytm",
	sum(a."Cash")+ sum(a."Card")+ sum(a."Upi")+ sum(a."Online")+ sum(a."Cheque")+ sum(a."Paytm") "PharmaSaleAmount" 
	from (
select a."AccountId", 
	 case when "PayTypeId"=1 then  coalesce(ar."OverallNetAmount",0) else 0 end "Cash"
	,case when "PayTypeId"=2 then  coalesce(ar."OverallNetAmount",0) else 0 end "Card"
	,case when "PayTypeId"=3 then  coalesce(ar."OverallNetAmount",0) else 0 end "Upi"
	,case when "PayTypeId"=4 then  coalesce(ar."OverallNetAmount",0) else 0 end "Online"
	,case when "PayTypeId"=5 then  coalesce(ar."OverallNetAmount",0) else 0 end "Cheque"
	,case when "PayTypeId"=6 then  coalesce(ar."OverallNetAmount",0) else 0 end "Paytm"
from "Account" a
left join "PharmacySaleHeader" ar on ar."CreatedBy" = a."AccountId"
left join "Provider" pr on pr."ProviderId"= ar."ProviderId"
left join "ProviderLocation" PL on PL."ProviderId"=pr."ProviderId" and case when "locationId" is null then 1=1 else  PL."LocationId"= "locationId" end
left join "Account" PA on PA."ReferenceId"=pr."ProviderId"  and PA."RoleId"=3
where (ar."SaleDate" >= f.weeks::text::date and ar."SaleDate" <= (f.weeks+ intervaldata)::text::date)  	
	and case when ("locationId" is null) or ("locationId"=0) then 1=1 else ar."LocationId"= "locationId" end
and case when ("referenceId" is null) or ("referenceId"=0) then 1=1 else PA."ReferenceId"= "referenceId" end
	)a
group by a."AccountId"
)
,PharmaReturnAmount as (
select a."AccountId",
	sum(a."Cash") "PharmaReturnCash",
	sum(a."Card") "PharmaReturnCard",
	sum(a."Upi") "PharmaReturnUpi",
	sum(a."Online") "PharmaReturnOnline",
	sum(a."Cheque") "PharmaReturnCheque",
	sum(a."Paytm") "PharmaReturnPaytm",
	sum(a."Cash")+ sum(a."Card")+ sum(a."Upi")+ sum(a."Online")+ sum(a."Cheque")+ sum(a."Paytm") "PharmaReturnAmount" 
	from(
select a."AccountId", 
	case when srh."PayTypeId"=1 then  coalesce(ar."OverallNetAmount",0) else 0 end "Cash"
	,case when srh."PayTypeId"=2 then  coalesce(ar."OverallNetAmount",0) else 0 end "Card"
	,case when srh."PayTypeId"=3 then  coalesce(ar."OverallNetAmount",0) else 0 end "Upi"
	,case when srh."PayTypeId"=4 then  coalesce(ar."OverallNetAmount",0) else 0 end "Online"
	,case when srh."PayTypeId"=5 then  coalesce(ar."OverallNetAmount",0) else 0 end "Cheque"
	,case when srh."PayTypeId"=6 then  coalesce(ar."OverallNetAmount",0) else 0 end "Paytm"
from "Account" a
left join "SaleReturnHeader" ar on ar."CreatedBy" = a."AccountId"
left join "PharmacySaleHeader" srh on srh."PharmacySaleHeaderId" = ar."PharmacySaleHeaderId"
where (ar."ReturnDate" >= f.weeks::text::date and ar."ReturnDate" <= (f.weeks+ intervaldata)::text::date) )a
group by a."AccountId"
)

select f.weeks,
	coalesce((select coalesce(sum(a."PharmaSaleAmount"),0) - coalesce(sum(b. "PharmaReturnAmount"),0) as  "PharmaAmount" 
from PharmaSaleAmount a
	left join PharmaReturnAmount b on a."AccountId"=b."AccountId"),0) ;															  
	
	raise notice 'findal data  %', f.weeks;									  
														
	end loop;
	RETURN QUERY
	select A.saledate,A.pharmacyamount from temp_table A;
	drop table temp_table;
END;
$BODY$;

ALTER FUNCTION public."widget_BarChart_Pharmacy_Sale_Revenue"(date, text, integer,integer,integer)
    OWNER TO postgres;
